package com.zbkj.common.utils.genutils;


import com.tiangong.framework.BusinessBus;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.gencode.mysql.ColumnEntity;
import com.zbkj.common.model.gencode.mysql.TableEntity;
import com.zbkj.common.utils.DateUtil;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 页面静态化
 */
public class GenHtmlUtils {
    public static Template initTemplatesLocation(String templateName) {
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        Template template = Velocity.getTemplate(templateName, "UTF-8");
        return template;
    }

    public static void generator(String templatePath, String fileName, VelocityContext context) {
        Template template = initTemplatesLocation(templatePath);
        FileWriter fw = null;
        try {
            fw = new FileWriter(fileName);
            template.merge(context, fw);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //generatorH5Html();
        //generatorIndexHtml();
    }

    public static void generatorMomentHtml() {
        int PageIndex = 1;
        int PageSize = 20;
        int nowSize = 0;
        do {
            Return ret = BusinessBus.handleService(
                    CDO.newRequest("pc", "getFriendsCircleList")
                            .setPageIndex(PageIndex)
                            .setPageSize(PageSize));
            CDO data = ret.getData();
            List<CDO> datalist = data.getCDOList("datalist");
            PageIndex++;
            nowSize = data.getIntValue("total");
            datalist.forEach(cdo -> {
                //TODO 页面静态化
                Return ret1 = CDO.newRequest("pc", "getFriendsCircleReplyListByFid")
                        .set("fid", cdo.getStringValue("cid"))
                        .set("cid", cdo.getStringValue("cid"))
                        .handleService();
                CDO cdoData = ret1.getData();

                String[] imgList = cdoData.getCDOValue("data").getStringValue("pictureaddr").split(",");

                VelocityContext context = new VelocityContext();
                context.put("data", cdoData.getCDOValue("data"));
                context.put("datalist", cdoData.getCDOListValue("datalist"));
                if (cdoData.getCDOValue("data").getStringValue("pictureaddr").length() > 0) {
                    context.put("imgList", imgList);
                }
                String path = "/Users/jinshunzhang/Desktop/游戏对接/PC端/generatorcode";
                if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                    path = "/usr/local/nginx/html/www";
                }
                generator("template/h5/moment.html.vm",
                        path + "/moment/" + cdo.getStringValue("cid") + ".html",
                        context
                );
            });
        } while (nowSize == PageSize);
    }

    public static void generatorGameInfoHtml() {
        int PageIndex = 1;
        int PageSize = 20;
        int nowSize = 0;
        do {
            Return ret = BusinessBus.handleService(
                    CDO.newRequest("pc", "getGameList")
                            .setPageIndex(PageIndex)
                            .setPageSize(PageSize));
            CDO data = ret.getData();
            List<CDO> datalist = data.getCDOList("datalist");
            PageIndex++;
            nowSize = datalist.size();
            datalist.forEach(cdo1 -> {
                //cdo.getCDOList("datalist").forEach(cdo1 -> {
                cdo1.set("score", (cdo1.getIntValue("score") + cdo1.getIntValue("scoreX")) / 10.0);
                if (cdo1.getDoubleValue("score") > 5.0) {
                    cdo1.set("score", "5.0");
                }
                cdo1.set("applyintroduce", cdo1.getStringValue("applyintroduce").replaceAll("\"", ""));
                cdo1.set("downloadnum", (cdo1.getIntValue("downloadnum") + cdo1.getIntValue("downloadnumx")));
                List<String> imgList = new ArrayList<>();
                if (cdo1.getStringValue("infoimg1long").length() != 0) {
                    imgList.add(cdo1.getStringValue("infoimg1long"));
                    imgList.add(cdo1.getStringValue("infoimg2long"));
                    imgList.add(cdo1.getStringValue("infoimg3long"));
                    imgList.add(cdo1.getStringValue("infoimg4long"));
                    imgList.add(cdo1.getStringValue("infoimg5long"));
                } else {
                    imgList.add(cdo1.getStringValue("infoimg1"));
                    imgList.add(cdo1.getStringValue("infoimg2"));
                    imgList.add(cdo1.getStringValue("infoimg3"));
                    imgList.add(cdo1.getStringValue("infoimg4"));
                    imgList.add(cdo1.getStringValue("infoimg5"));
                }
                cdo1.set("imgList", imgList);
                cdo1.set("applyintroduce", cdo1.getStringValue("applyintroduce").replaceAll("\\n", "<br/>"));
                Return ret2 = CDO.newRequest("pc", "getGameComment")
                        .set("softwareId", cdo1.getStringValue("softwareid"))
                        .set("pageSize", 10)
                        .handleService();

                cdo1.set("commentList", ret2.getData().getCDOListValue("datalist"));
                VelocityContext context = new VelocityContext();
                context.put("data", cdo1);
                String path = "/Users/jinshunzhang/Desktop/游戏对接/PC端/generatorcode";
                if (System.getProperty("os.name").toLowerCase().contains("linux")) {
                    path = "/usr/local/nginx/html/www";
                }
                generator("template/h5/gameinfo.html.vm",
                        path + "/game/" + cdo1.getStringValue("softwareid") + ".html",
                        context
                );
                // });
            });
        } while (nowSize == PageSize);
    }

    public static void generatorRankingsHtml() {

        Return ret = CDO.newRequest("pc", "getRankingList").handleService();
        Return ret1 = CDO.newRequest("pc", "getRankingListMore").handleService();


        List<CDO> groupList = ret.getData().getCDOValue("data").getCDOList("datalist");
        List<CDO> allgameList = new ArrayList<>();
        groupList.forEach(cdo1 -> {
            allgameList.addAll(cdo1.getCDOList("dataList"));
        });

        List<CDO> gameList = new ArrayList<>();
        gameList.addAll(groupList.get(0).getCDOList("dataList"));
        gameList.addAll(groupList.get(1).getCDOList("dataList"));

        allgameList.forEach(cdo1 -> {
            cdo1.set("score", (cdo1.getIntValue("score") + cdo1.getIntValue("scoreX")) / 10.0);
            if (cdo1.getDoubleValue("score") > 5.0) {
                cdo1.set("score", "5.0");
            }
        });

        VelocityContext context = new VelocityContext();
        context.put("allgameList", allgameList);
        context.put("gameList", gameList);
        generator("template/h5/rankings.html.vm",
                "/Users/jinshunzhang/workspace/gamepro/datacenter/datacenter-system/src/main/resources/template/h5/rankings123.html",
                context
        );
    }

    public static void generatorH5Html() {
        Return ret = BusinessBus.handleService(
                CDO.newRequest("pc", "getIndexGame")
                        .set("module", 2)
                        .set("system", 3)
        );
        CDO cdoData = ret.getData();
        List<CDO> groupList = cdoData.getCDOValue("data").getCDOList("datalist");

        List<CDO> allgameList = new ArrayList<>();
        groupList.forEach(cdo1 -> {
            allgameList.addAll(cdo1.getCDOList("dataList"));
        });

        List<CDO> gameList = new ArrayList<>();
        gameList.addAll(groupList.get(0).getCDOList("dataList"));
        gameList.addAll(groupList.get(1).getCDOList("dataList"));

        allgameList.forEach(cdo1 -> {
            cdo1.set("score", (cdo1.getIntValue("score") + cdo1.getIntValue("scoreX")) / 10.0);
            if (cdo1.getDoubleValue("score") > 5.0) {
                cdo1.set("score", "5.0");
            }
        });

        VelocityContext context = new VelocityContext();
        context.put("allgameList", allgameList);
        context.put("gameList", gameList);
        generator("template/h5/h5index.html.vm",
                "/Users/jinshunzhang/workspace/gamepro/datacenter/datacenter-system/src/main/resources/template/h5/4html.html",
                context
        );
    }

    public static void generatorIndexHtml() {
        Return ret0 =
                CDO.newRequest("pc", "getBannerList")
                        .set("imgblock", 2).setPageSize(100)
                        .handleService();
        List<CDO> banner1List = ret0.getData().getCDOListValue("datalist");

        Return ret00 =
                CDO.newRequest("pc", "getBannerList")
                        .set("imgblock", 3).setPageSize(100)
                        .handleService();
        List<CDO> banner2List = ret00.getData().getCDOListValue("datalist");
        Return ret =
                CDO.newRequest("pc", "getIndexGame")
                        .set("module", 1)
                        .set("system", 1)
                        .handleService();
        CDO cdoData = ret.getData();
        List<CDO> groupList = cdoData.getCDOList("datalist");

        groupList.forEach(cdo1 -> {
            cdo1.getCDOList("dataList").forEach(cdo2 -> {
                cdo2.set("score", (cdo2.getIntValue("score") + cdo2.getIntValue("scoreX")) / 10.0);
                if (cdo2.getDoubleValue("score") > 5.0) {
                    cdo2.set("score", "5.0");
                }
            });
        });
        banner1List.forEach(cdo -> {
            cdo.set("imgaddress",cdo.getStringValue("imgadderss").length()==0?"#":cdo.getStringValue("imgadderss"));
        });
        banner2List.forEach(cdo -> {
            cdo.set("imgaddress",cdo.getStringValue("imgadderss").length()==0?"#":cdo.getStringValue("imgadderss"));
        });

        VelocityContext context = new VelocityContext();
        context.put("groupList", groupList);
        context.put("banner1List", banner1List);
        context.put("banner2List", banner2List);
//        generator("template/h5/index.html.vm",
//                "/Users/jinshunzhang/workspace/gamepro/datacenter/datacenter-system/src/main/resources/template/h5/21html.html",
//                context

        String path = "/Users/jinshunzhang/Desktop/游戏对接/PC端/generatorcode";
        if (System.getProperty("os.name").toLowerCase().contains("linux")) {
            path = "/usr/local/nginx/html/www";
        }
        generator("template/h5/index.html.vm",
                path + "/" + "index.html",
                context
        );
    }


}
